<template>
  <j-modal
    :title="title"
    :width="600"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="选择群组">
          <a-tree-select
            style="width: 100%"
            v-model="form.labelId"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            placeholder="请选择"
            allow-clear
            tree-default-expand-all
            :treeData="treeDatas"
            @change="onChange"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button key="submit" type="primary" @click="handleOk">确定</a-button>
    </template>
  </j-modal>
</template>

<script>
import * as peopleApi from '@/api/wxSmuggle/people.js'
export default {
  name: 'chooseGroupsModal',
  props: {
    title: {
      type: String,
      default: '修改分组'
    },
    treeDatas: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      disableSubmit: false,
      form: {},
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
      confirmLoading: false
    }
  },
  methods: {
    add() {
      this.visible = true
      this.$nextTick(() => {})
    },
    edit(record) {
      this.$set(this.form, 'idList', record)
      this.visible = true
      this.$nextTick(() => {})
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      this.$refs.form.validate(valid => {
        if (valid) {
          peopleApi
            .wxSeedPersonEdit(this.form)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.handleCancel()
              } else {
                that.$message.warn(res.message)
              }
            })
            .catch(e => {
              that.$message.warn('操作失败！')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    onChange(value, values) {
      let labelName = values.join(',')
      this.$set(this.form, 'labelName', labelName)
    }
  }
}
</script>
<style lang="less" scoped></style>
